package com.xcx.services.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.xcx.bo.PetSound;
import com.xcx.bo.PetSoundExample;
import com.xcx.common.dto.base.BaseDto;
import com.xcx.common.dto.base.BaseResp;
import com.xcx.common.dto.req.SaveUserInfoReq;
import com.xcx.common.dto.req.UserLoginReq;
import com.xcx.common.dto.resp.UserLoginResp;
import com.xcx.common.enums.IsValidEnum;
import com.xcx.common.enums.ProductCodeEnum;
import com.xcx.common.services.PetService;
import com.xcx.common.services.common.CommUserService;
import com.xcx.common.utils.CheckParamUtils;
import com.xcx.common.utils.ExceptionUtils;
import com.xcx.common.utils.RandomUtil;
import com.xcx.common.utils.StringUtils;
import com.xcx.dao.PetSoundDao;
import com.xcx.dto.QuerySoundListReq;
import com.xcx.dto.QuerySoundListResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Component
public class PetServiceImpl implements PetService {


    @Value("${wx.catAppId}")
    private String catAppId;

    @Value("${wx.catAppSecret}")
    private String catAppSecret;

    @Value("${wx.dogAppId}")
    private String dogAppId;

    @Value("${wx.dogAppSecret}")
    private String dogAppSecret;


    @Reference
    private CommUserService commUserService;

    @Resource
    private PetSoundDao petSoundDao;

    @Override
    public String userLogin(String reqStr) {
        log.info("userLogin.req:{}", reqStr);
        UserLoginResp UserLoginResp = new UserLoginResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            UserLoginReq userLoginReq = BaseDto.fromJson(reqStr, UserLoginReq.class);
            if (StringUtils.equals(ProductCodeEnum.PET_CAT_CODE.getCode(), userLoginReq.getProductCode())) {
                userLoginReq.setAppId(catAppId);
                userLoginReq.setSecret(catAppSecret);
            } else {
                userLoginReq.setAppId(dogAppId);
                userLoginReq.setSecret(dogAppSecret);
            }
            CheckParamUtils.validateReqGson(userLoginReq);
            UserLoginResp = commUserService.userLogin(userLoginReq);

            UserLoginResp.buildDefaultSuccessResp(userLoginReq);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("PetServiceImpl", reqStr, UserLoginResp, e.getMessage(), e);
        }
        log.info("userLogin.resp:{}", UserLoginResp.toString());
        return UserLoginResp.toString();
    }


    @Override
    public String saveUserInfo(String reqStr) {
        log.info("saveUserInfo.req:{}", reqStr);
        BaseResp baseResp = new BaseResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            SaveUserInfoReq saveUserInfoReq = BaseDto.fromJson(reqStr, SaveUserInfoReq.class);
            CheckParamUtils.validateReqGson(saveUserInfoReq);
            baseResp = commUserService.saveUserInfo(saveUserInfoReq);

            baseResp.buildDefaultSuccessResp(saveUserInfoReq);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("PetServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("saveUserInfo.resp:{}", baseResp.toString());
        return baseResp.toString();
    }

    /**
     * 生成录音信息
     *
     * @param reqStr
     * @return
     */
    @Override
    public String querySound(String reqStr) {
        log.info("querySound.req:{}", reqStr);
        QuerySoundListResp baseResp = new QuerySoundListResp();
        try {
            CheckParamUtils.validateReqString(reqStr);
            QuerySoundListReq baseReq = BaseDto.fromJson(reqStr, QuerySoundListReq.class);
            CheckParamUtils.validateReqGson(baseReq);
            baseResp.setSoundList(query(baseReq.getProductCode(), baseReq.getTime()));
            baseResp.setReqNo(baseReq.getReqNo());

            baseResp.buildDefaultSuccessResp(baseReq);
        } catch (Exception e) {
            ExceptionUtils.buildExceptionResp("PetServiceImpl", reqStr, baseResp, e.getMessage(), e);
        }
        log.info("querySound.resp:{}", baseResp.toString());
        return baseResp.toString();

    }

    public List<String> query(String productCode, String time) {
        PetSoundExample example = new PetSoundExample();
        example.createCriteria()
                .andProductCodeEqualTo(productCode)
                .andStatusEqualTo(IsValidEnum.VALID_YES.getCode());
        List<PetSound> lst = petSoundDao.selectByExample(example);
        lst = RandomUtil.getRandomList(lst, Integer.parseInt(time));

        List<String> newLst = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(lst)) {
            for (PetSound petSound : lst) {
                newLst.add(petSound.getSoundUrl());
            }
        }
        return newLst;
    }


}
